package com.homework.work04;

public class Recursion {

    //递归计算出1-100的和
    void num(int i, int j) {
        if (i < 100) {
            j = j + i;
            i++;
            num(i, j);
        }
        if (i == 100) {
            j = j + i;
            System.out.println(j);
        }
    }

    //递归求阶乘(n!)的算法
    int sum(int n) {
        if (n == 0) {
            return 1;
        }
        return n * sum(n - 1);
    }

    //递归斐波那契数列
    int fibonacci(int index) {
        // 设置结束递归的限制条件
        if (index == 1 || index == 2) {
            return 1;
        }
        // F(index) = F(index-1) + F(index-2)
        return fibonacci(index - 1) + fibonacci(index - 2);
    }

    public static void main(String[] args){
        Recursion recursion = new Recursion();
        recursion.num(1, 0);
        int sum = recursion.sum(10);
        System.out.println(sum);
        int fibonacci = recursion.fibonacci(10);
        System.out.println(fibonacci);
        // 输出前 10 个数
        for (int i = 1; i <= 10; i++) {
            System.out.println(recursion.fibonacci(i));
        }
    }
}
